package com.example.study1206.controller;

import com.example.study1206.Entity.Information;
import com.example.study1206.Utils.SpringContextUtil;
import com.example.study1206.service.MultipleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@RestController
@RequestMapping("/multiTest")
public class MultipleController {



    @PostMapping("/multiInvoke")
    public Object invoke(@RequestBody Information information) throws InvocationTargetException, IllegalAccessException {
        MultipleService multipleService = (MultipleService) SpringContextUtil.getBean(information.getServiceType());
        Method[] methods = multipleService.getClass().getDeclaredMethods();
        Method does = null;
        for (Method m : methods) {
            if (information.getMethodName().equals(m.getName())) {
                does = m;
                break;
            }
        }
        if (does == null) {
            return "No Method Found";
        }
        try {
            if (information.getSimpleParams() != null) {
                return does.invoke(multipleService, information.getSimpleParams());
            } else {
                return does.invoke(multipleService);
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return "Invalid Argument";
        }

    }
//    @PostMapping("/multiInvoke")
//    public Object invoke(@RequestBody Information2 information) throws InvocationTargetException, IllegalAccessException {
//        MultipleService multipleService = (MultipleService) SpringContextUtil.getBean(information.getServiceType());
//        Method[] methods = multipleService.getClass().getDeclaredMethods();
//        Method does = null;
//        for (Method m : methods) {
//            if (information.getMethodName().equals(m.getName())) {
//                does = m;
//                break;
//            }
//        }
//        if (does == null) {
//            return "No Method Found";
//        }
//        try {
//            if (information.getRequirement()!= null) {
//                return does.invoke(multipleService, information.getRequirement());
//            }
//            return does.invoke(multipleService);
//        } catch (IllegalArgumentException e) {
//            e.printStackTrace();
//            return "Invalid Argument";
//        }
//
//    }


//    @GetMapping("testOnly")
//    public String testOnly() throws IOException {
//        InputStream f = new FileInputStream("C:\\Users\\Asus\\Desktop\\doubles.xlsx");
//        XSSFSheet sheet;
//        try {
//            sheet = newServiceimp.prepareForUpload(f, "doubles");
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "ExcelHeaderFormatIncorrect";
//
//        }
//        try {
//            newServiceimp.uploadFile(sheet, "doubles");
//        }catch (Exception e){
//            e.printStackTrace();
//            return "InsertException. Make sure the first column of excel represents Primary Key\n" +
//                    " and make sure excel matched the table exactly";
//        }
//
//        return "OK";
//    }

}
